package scau.markmapscau.View;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.text.Font;
import javafx.embed.swing.SwingFXUtils;
import javafx.stage.Modality;
import javafx.stage.Stage;

import scau.markmapscau.LaunchMain;
import scau.markmapscau.Model.Node.Node;
import scau.markmapscau.Model.Node.NodeArranger;
import scau.markmapscau.Model.Tool.ToolArranger;
import scau.markmapscau.Model.Tree.TreeArranger;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.concurrent.LinkedBlockingQueue;


//前端设计者需要同时完成思维导图的绘画工作、 树状图区的绘画工作
public class ViewerArranger {
    public NodeArranger nodeArranger;//节点管理者
    public TreeArranger treeArranger;//树状图管理者
    public ToolArranger toolArranger;//工具栏管理者
    Stage primaryStage;
    Parent root;              //FXML文件管理者
    AnchorPane anchorPane;    //画思维导图的区域
    ScrollPane scrollPane;
    AnchorPane smallPane;//画缩略图
    Pane pane;//画结构树的区域
    AnchorPane toolPane;
    Button add;//添加按钮
    Button delete;
    ChoiceBox choiceStyleBox;
    Label title;
    ColorPicker colorPickerOut;
    private boolean hasRightChild =true;
    private boolean hasLeftChild  =true;
    private double rate = 1;

    public int selectNum=-1;
    public static double X_EXTENDS=20;//伸出的基础线长度
    //public static double Y_THRESHOLD=20;//threshold

    public ViewerArranger(NodeArranger nodeArranger, TreeArranger treeArranger, ToolArranger toolArranger,
                          AnchorPane anchorPane    , ScrollPane scrollPane    , Pane pane, AnchorPane smallPane,
                          Button add, Button delete, ChoiceBox choiceStyleBox, AnchorPane toolPane,
                          Label title,ColorPicker colorPicker,Stage primaryStage){
        this.nodeArranger = nodeArranger;
        this.treeArranger = treeArranger;
        this.toolArranger = toolArranger;
        this.anchorPane = anchorPane;
        this.scrollPane = scrollPane;
        this.pane=pane;//结构区
        this.smallPane = smallPane;//缩略图区
        this.add=add;
        this.delete=delete;
        this.choiceStyleBox = choiceStyleBox;
        this.toolPane=toolPane;
        this.title =title;
        this.primaryStage = primaryStage;
        this.colorPickerOut = colorPicker;
    }


    /*以下是思维导图的绘画工作函数*/
    //画出单个节点
    public void drawSingleNode(Node node){
        //1.首先确定为节画好形状,选定为矩形,TODO:可以通过image来替换
        TextField textField = setTextFile(node,this.nodeArranger.getNodeList(),colorPickerOut);//根据node的信息来设置该文本输入框
        textField.setContextMenu(setContextMenuItem(node.getId()));//设置菜单
        // Color color=new Color();//保存的颜色
        //！！！！！！这三个要在哈希表中记录
        textField.setStyle("-fx-background-color:"+node.getColor());//显示颜色
        textField.setFont(new Font(node.getStyle(),node.getSize()));//显示样式和大小
        System.out.println("selectNum:"+selectNum);
        if(selectNum==node.getId()){//2.为该节点添加红色边框
            textField.getStyleClass().add("text-field2");
        }
        this.anchorPane.getChildren().add(textField);
    }

    public ContextMenu setContextMenuItem(int id){
        ContextMenu contextMenu=new ContextMenu();
        //添加子节点
        ImageView imageView1=new ImageView(new Image("file:src/main/resources/scau/markmapscau/image/edit.png"));
        imageView1.setFitWidth(15);
        imageView1.setFitHeight(15);
        contextMenu.getItems().add(0,new MenuItem("  编辑                                 ",imageView1));
        contextMenu.getItems().get(0).setOnAction(actionEvent -> {
            //弹出编辑框
            Stage addStage = new Stage();
            addStage.setHeight(400);
            addStage.setWidth(500);
            AnchorPane anchorPane = new AnchorPane();
            Parent root;
            try{
                FXMLLoader fxmlLoader = new FXMLLoader(LaunchMain.class.getResource("addStage.fxml"));
                root = fxmlLoader.load();
                Scene scene = new Scene(root, 1000,1000 );
                //addStage.initStyle(StageStyle.TRANSPARENT);
                addStage.setWidth(700);
                addStage.setHeight(500);
                addStage.setResizable(false);
                //scene.getStylesheets().add(LaunchMain.class.getResource("addStage.css").toExternalForm());//添加css文件
                scene.setFill(Color.TRANSPARENT);
                addStage.initOwner(primaryStage);
                addStage.initModality(Modality.WINDOW_MODAL);
                addStage.setScene(scene);
                Button confirmButton = (Button) scene.lookup("#confirmButton");
                //Button closeButton = (Button)scene.lookup("#closeButton") ;
                ChoiceBox fontStyleChoiceBox = (ChoiceBox)scene.lookup("#fontStyle");
                ChoiceBox fontSizeChoiceBox = (ChoiceBox)scene.lookup("#fontSize");
                fontSizeChoiceBox.setValue((int)this.nodeArranger.getNodeList().get(id).getSize());
                TextField textField = (TextField)scene.lookup("#text");
                textField.setText(this.nodeArranger.getNodeList().get(id).getContent());
                ColorPicker colorPicker = (ColorPicker)scene.lookup("#ColorPickerChose");
                colorPicker.setValue(Color.web(this.nodeArranger.getNodeList().get(id).getColor()));
                String[] names = {"FangSong","HeiTi", "KaiTi", "FangSong_GB2312", "KaiTi_GB2312", "Microsoft JhengHei", "Microsoft YaHei"};
                String[] names_ = {"仿宋","黑体","楷体","仿宋_GB2312", "楷体_GB2312", "微軟正黑體", "微软雅黑体"};
                for(int i = 0;i<names.length;i++){
                    if(this.nodeArranger.getNodeList().get(id).getStyle().equals(names[i])){
                        fontStyleChoiceBox.setValue((String)(names_[i]));
                        break;
                    }
                }
                for(int i = 0;i<names_.length;i++){
                    fontStyleChoiceBox.getItems().add(i,names_[i]);
                }
                for(int i = 10;i<35;i++){
                    fontSizeChoiceBox.getItems().add(i);
                }
                fontSizeChoiceBox.setValue(nodeArranger.getNodeList().get(this.selectNum).getSize());
                confirmButton.setOnAction(a->{
                    Node selectNode = nodeArranger.getNodeList().get(this.selectNum);
                    // 1.获取文本框内容
                    selectNode.setContent(textField.getText());
                    // 2.获取节点背景色
                    Color color=colorPicker.getValue();
                    String hex1 = Integer.toHexString(color.hashCode());
                    selectNode.setColor("#"+hex1);
                    this.colorPickerOut.setValue(Color.web("#"+hex1));
                    // 3.获取字体样式
                    int idx = 0;
                    for(int i = 0 ;i< names_.length;i++){
                        if(names_[i].equals((String) fontStyleChoiceBox.getValue())){
                            idx = i;
                            break;
                        }
                    }
                    selectNode.setStyle(names[idx]);
                    // 4.获取字体大小
                    selectNode.setSize((Integer)fontSizeChoiceBox.getValue());
                    this.drawNodesAndLines(this.nodeArranger.getNodeList());
                    this.drawSmallPane();
                    addStage.close();
                });
                addStage.show();
            }catch (IOException e){
                e.printStackTrace();
            }
        });
        ImageView imageView2=new ImageView(new Image("file:src/main/resources/scau/markmapscau/image/add3.png"));
        imageView2.setFitWidth(15);
        imageView2.setFitHeight(15);
        contextMenu.getItems().add(1,new MenuItem("  添加子节点                            ",imageView2));
        contextMenu.getItems().get(1).setOnAction(a->{
            //add
            System.out.println("点击了新建子节点事件:"+selectNum+" menu");
            newNodeByParent(id);
            drawNodesAndLines(nodeArranger.getNodeList());
        });

        ImageView imageView3=new ImageView(new Image("file:src/main/resources/scau/markmapscau/image/delete3.png"));
        imageView3.setFitWidth(15);
        imageView3.setFitHeight(15);
        contextMenu.getItems().add(2,new MenuItem("  删除节点          ",imageView3));
        contextMenu.getItems().get(2).setOnAction(a->{
            deleteNode();
            nodeArranger.arrangeLayout();
            drawNodesAndLines(nodeArranger.getNodeList());
        });
        //添加兄弟节点
        ImageView imageView4=new ImageView(new Image("file:src/main/resources/scau/markmapscau/image/brother.png"));
        imageView4.setFitWidth(15);
        imageView4.setFitHeight(15);
        contextMenu.getItems().add(3, new MenuItem("  添加兄弟节点          ",imageView4));
        contextMenu.getItems().get(3).setOnAction(a -> {
            int parentId=nodeArranger.getNodeList().get(id).getParentId();
            System.out.println("点击了新建子节点事件:"+id+" menu");
            newNodeByParent(parentId);
            drawNodesAndLines(nodeArranger.getNodeList());
        });
        if(id == 0){
            contextMenu.getItems().get(2).setDisable(true);
            contextMenu.getItems().get(3).setDisable(true);
        }
        return contextMenu;
    }


    public TextField setTextFile(Node node, LinkedHashMap<Integer,Node> nodeList,ColorPicker colorPickerOut){
        TextField textField = new TextField();
        //设置内容
        //String debugContent = String.valueOf(node.getId())+" "+String.valueOf(node.getNodeLocationY())+" "+String.valueOf(node.getNodeBlockHeight())+" "+String.valueOf(node.getNodeHeight());
        textField.setId(node.getId()+"text");
        textField.setText(node.getContent());//文本
        //设置在图上的显示
        //1.layoutX和layoutY是相对于父节点的0,0（origo）的偏移量
        textField.setLayoutX(node.getNodeLocationX());
        textField.setLayoutY(node.getNodeLocationY());
        //2.设置文本框的高度和宽度
        textField.setPrefHeight(node.getNodeHeight());
        textField.setPrefWidth(node.getNodeWidth());
        //3.设置文本框的文字排列行数
        textField.setPrefColumnCount(1);
        //4.设置文字是否可见
        textField.setVisible(true);
        //5.设置为10号字体+
        //textField.setFont(new Font("HeiTi",17));
        //8.设置文字位于中央
        textField.setAlignment(Pos.CENTER);
        //9.设置圆角

        //6.添加重载函数,绑定一个点击文本框后,文本框就被选中的功能
        textField.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                MouseButton mouseButton =mouseEvent.getButton();
                if(mouseButton!= MouseButton.PRIMARY && mouseButton!=MouseButton.SECONDARY){
                    return ;
                }
                if(selectNum == -1){
                    selectNum = node.getId();//选中的node
                    drawNodesAndLines(nodeList);//重新画图
                    //System.out.println("用户点击了框图"+selectNum);
                }else if(selectNum!= node.getId()){
                    //System.out.println("用户点击了别的框图:"+selectNum);
                    //2.修改选中节点为当前的节点
                    selectNum = node.getId();
                    //System.out.println("#"+node.getColor());
                    colorPickerOut.setValue(Color.web(node.getColor()));
                    //3.重新画图
                    drawNodesAndLines(nodeList);
                }
                //选中更改图标颜色
                ImageView imageView=new ImageView(new Image("file:src/main/resources/scau/markmapscau/image/add2.png"));
                imageView.setFitHeight(22);
                imageView.setFitWidth(22);
                add.setGraphic(imageView);
                ImageView imageView2=new ImageView(new Image("file:src/main/resources/scau/markmapscau/image/delete2.png"));
                imageView2.setFitHeight(22);
                imageView2.setFitWidth(22);
                if(selectNum!=0)
                    delete.setGraphic(imageView2);
                else
                    delete.setGraphic(imageView2);
                mouseEvent.consume();
            }
        });

        //7.添加重载函数,绑定一个监听文本框内文本变化的函数
        textField.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observableValue, String s, String t1) {
                node.setContent(t1);
                textField.setText(t1);
                drawTree(nodeList);//TODO:可能需要改动
            }
        });
        return textField;
    }
    //直接画图的函数
    public void drawNodesAndLines(HashMap<Integer,Node> nodeList){
        anchorPane.getChildren().clear();
        nodeArranger.arrangeLayout();
        drawNodes(nodeList);
        drawLines();
        drawSmallPane();
    }
    public void drawNodes(HashMap<Integer,Node> nodeList){
        this.anchorPane.getChildren().clear();
        //遍历节点链表,画图
        //this.nodeArranger.deBug();
//        this.nodeArranger.getNodeList().forEach((key,value)->{
        nodeList.forEach((key,value)->{
            this.drawSingleNode(value);
        });
        drawTree(nodeList);
    }

    public void newNodeByParent(int parentId){
        ViewerArranger viewerArranger = LaunchMain.viewerArranger;
        viewerArranger.nodeArranger.newNode(parentId);
    }

    public void deleteNode(){
        if(this.selectNum == -1){
            return;
        }
//        nodeArranger.getNodeList().get(selectNum).setIsChose(false);//未被选中
        System.out.println("当前被选中的节点为:"+selectNum);
        ViewerArranger viewerArranger = LaunchMain.viewerArranger;
        viewerArranger.nodeArranger.deleteNode(this.selectNum);
    }



    /*以下是画线的主要代码*/
    //在这里可以讲解一下画线的方式,主要思路是"一出一折一连"
    //一出:指的是先出从父亲节点出一个根线,以这根线为主线开始分布其子节点的线
    //记录下"一出"出的线的长度,记录下该点，然后开始扩张
    //接下来是公式的计算,首先计算右侧边布局的
    //伸出一段距离,伸出该段,drawingPointX=parentNode.getX()+parentNode.getWidth()
    //伸出该段后,就开始各子节点的线
    //一折:首先从该drawingPointX开始向上向下延伸,到达所有子节点的Y坐标
    //一连:到达该子节点Y坐标后,然后直接X轴方向连过去就可以了
    public void connectLineRight(int drawingNode){//右侧布局画线代码
        //核心算法是广搜,设置队列,备忘:(1) 出队队头(2)入队子节点
        ArrayList<Line> lineArray = new ArrayList<>();//计算得到的线的集合
        LinkedBlockingQueue nodeQueue = new LinkedBlockingQueue<Integer>();//存储目前还没有被遍历的节点编号
        nodeQueue.offer(drawingNode);
        while(!nodeQueue.isEmpty()){
            int parentId = (int)nodeQueue.poll();//拆箱
            Node parentNode =  this.nodeArranger.getNodeList().get(parentId);
            //然后搜索子节点列表
            if(parentNode.getSonNodeList().size() == 0){
                continue;
            }
            if(parentNode.getId() == 0 && !this.hasRightChild){
                continue;
            }
            double parentNodeCenterY=(parentNode.getNodeLocationY()*2+parentNode.getNodeHeight())/2;
            double parentNodeX = parentNode.getNodeLocationX()+parentNode.getNodeWidth();
            //得到drawingPoint
            Line beginLine = new Line(parentNodeX,parentNodeCenterY,parentNodeX+X_EXTENDS,parentNodeCenterY);//起点(x1,y1),终点(x2,y2)
            lineArray.add(beginLine);
            double drawingPointX = parentNodeX+X_EXTENDS;//父亲节点的横坐标+一段偏移
            double drawingPointY = parentNodeCenterY;//父亲节点的中心Y坐标
            for(Integer id:parentNode.getSonNodeList()){
                //1.计算父亲节点的中心位置和子节点的中心位置的差值
                if(parentNode.getSonNodeList().size() ==1){
                    Node sonNode = this.nodeArranger.getNodeList().get(id);
                    Line newLine  = new Line(drawingPointX,parentNodeCenterY,sonNode.getNodeLocationX(),parentNodeCenterY);
                    lineArray.add(newLine);
                }else{
                    Node sonNode = this.nodeArranger.getNodeList().get(id);
                    if(!checkLegal(1,sonNode.getNodeLocationX(),parentNode.getNodeLocationX())){
                        continue;
                    }
                    double sonNodeCenterY=(sonNode.getNodeLocationY()*2+sonNode.getNodeHeight())/2;
                    double sonNodeX = sonNode.getNodeLocationX();
                    Line newLineY = new Line(drawingPointX,drawingPointY,drawingPointX,sonNodeCenterY);
                    lineArray.add(newLineY);
                    //更新画图指针
                    drawingPointY = sonNodeCenterY;
                    Line newLineX = new Line(drawingPointX,drawingPointY,sonNodeX,sonNodeCenterY);
                    lineArray.add(newLineX);
                }
                nodeQueue.offer(id);
            }
        }
        for(Line line:lineArray){
            //对绘图图进行修改时，不能同时进行
            this.anchorPane.getChildren().add(line);
        }
    }

    public void connectLineLeft(int drawingNode){//左侧布局代码计算
        //核心算法是广搜,设置队列,备忘:(1) 出队队头(2)入队子节点
        ArrayList<Line> lineArray = new ArrayList<>();//计算得到的线的集合
        LinkedBlockingQueue nodeQueue = new LinkedBlockingQueue<Integer>();//存储目前还没有被遍历的节点编号
        nodeQueue.offer(drawingNode);
        while(!nodeQueue.isEmpty()){
            int parentId = (int)nodeQueue.poll();//拆箱
            Node parentNode =  this.nodeArranger.getNodeList().get(parentId);
            //然后搜索子节点列表
            if(parentNode.getSonNodeList().size() == 0){
                continue;
            }
            if(parentNode.getId() ==0 && !this.hasLeftChild){
                continue;
            }
            double parentNodeCenterY=(parentNode.getNodeLocationY()*2+parentNode.getNodeHeight())/2;
            double parentNodeX = parentNode.getNodeLocationX();
            //得到drawingPoint
            Line beginLine = new Line(parentNodeX,parentNodeCenterY,parentNodeX-X_EXTENDS,parentNodeCenterY);//起点(x1,y1),终点(x2,y2)
            double drawingPointX = parentNodeX-X_EXTENDS;double drawingPointY = parentNodeCenterY;
            lineArray.add(beginLine);
            for(Integer id:parentNode.getSonNodeList()){
                if(parentNode.getSonNodeList().size() == 1){
                    Node sonNode = this.nodeArranger.getNodeList().get(id);
                    Line newLine  = new Line(drawingPointX,parentNodeCenterY,sonNode.getNodeLocationX()+sonNode.getNodeWidth(),parentNodeCenterY);
                    lineArray.add(newLine);
                }else{
                    //1.计算父亲节点的中心位置和子节点的中心位置的差值
                    Node sonNode = this.nodeArranger.getNodeList().get(id);
                    if(!checkLegal(2,sonNode.getNodeLocationX(),parentNode.getNodeLocationX())){
                        continue;
                    }
                    double sonNodeCenterY=(sonNode.getNodeLocationY()*2+sonNode.getNodeHeight())/2;
                    double sonNodeX = sonNode.getNodeLocationX()+sonNode.getNodeWidth();
                    Line newLineY = new Line(drawingPointX,drawingPointY,drawingPointX,sonNodeCenterY);
                    lineArray.add(newLineY);
                    //更新画图指针
                    drawingPointY = sonNodeCenterY;
                    Line newLineX = new Line(drawingPointX,drawingPointY,sonNodeX,sonNodeCenterY);
                    lineArray.add(newLineX);
                }
                nodeQueue.offer(id);
            }
        }
        for(Line line:lineArray){
            this.anchorPane.getChildren().add(line);
        }
    }

    public void connectLineAuto(){//中间布局,和之前比较不同,画线的方式也是和之前的是一样的,是根据子节点坐标来确定
        //检查父亲节点的孩子情况,检查是否有左右子树
        if(this.nodeArranger.getNodeList().size() == 0){
            return ;
        }
        hasLeftChild = false;
        hasRightChild = false;
        Node parentNode = this.nodeArranger.getNodeList().get(0);
        for(int id:parentNode.getSonNodeList()){
            Node node = this.nodeArranger.getNodeList().get(id);
            //对绘图图进行修改时，不能同时进行
            if(parentNode.getNodeLocationX()>node.getNodeLocationX()){//当父亲节点的X坐标大于孩子节点坐标,就是左侧布局
                hasLeftChild =true;//左侧布局就有左孩子
            }else{
                hasRightChild = true;
            }
        }
        connectLineRight(0);
        connectLineLeft(0);
    }


    public void drawLines(){
        ViewerArranger viewerArranger = LaunchMain.viewerArranger;
        switch (viewerArranger.nodeArranger.getArrangeMode()){
            case RIGHT ->{this.hasRightChild = true;this.connectLineRight(0);break;}
            case LEFT  ->{this.hasLeftChild = true;this.connectLineLeft(0);break;}
            case AUTO  ->{this.connectLineAuto();break;}
            default -> {break;}
        }
    }

    private boolean checkLegal(int mode,double sonX,double parentX){
        boolean flag =true;
        if(mode ==1){//右侧布局,此时要求父亲节点的X坐标要比子节点的坐标要小
            if(parentX>sonX){
                flag = false;
            }
        }else if(mode == 2){//左侧布局,此时要求父亲的节点的X坐标要比子节点的坐标要大
            if(parentX<sonX){
                flag = false;
            }
        }
        return flag;
    }

    //修改界面缩放
    public int setScale(int rate,int state){
        if(state == 1){//放大
            rate += 10;
        }
        if(state == 2){//缩小
            rate -= 10;
        }
        if(rate>=200){//最大是放大两倍
            rate = 200;
        }
        if(rate<=50){//最小是缩小一半
            rate = 50;
        }
        this.nodeArranger.setScale(rate/100.0);
        this.rate = (rate/100);
        return rate;
    }



    /*以下是添加右键出现菜单的功能*/
    //zzzz
    /*结构区绘图*/
    //每删除、添加节点就遍历链表并画图
    public void drawTree(HashMap<Integer,Node> nodeList) {
        //1.先清空pane
        this.pane.getChildren().clear();
        //2.遍历节点，画图
//        HashMap<Integer,Node> nodelist=this.nodeArranger.getNodeList();
        System.out.println(nodeList);
        //因为直接对Pane进行了设置，直接刷新了UI，但操作只能在JavaFX Application Thread(单线程)上
        //调用方法在任何现线程执行场景元素的操作,可能会出现结点序号错误的的情况
        TreeItem treeItem=new TreeItem<>(nodeList.get(0).getContent());//该节点为根部节点
        treeItem.setExpanded(true);
        goThrough(nodeList,nodeList.get(0),treeItem);//从第一个节点开始遍历
        TreeView treeView=new TreeView<>(treeItem);
        treeView.setPrefWidth(this.pane.getPrefWidth());
        System.out.println(this.pane.getHeight());
        // treeView.setPrefHeight(this.pane.getHeight());
        treeView.getStyleClass().add("noborder");//设置无选中蓝色边框
        treeView.setLayoutY(10);
        this.pane.getChildren().add(treeView);
        //3.遍历完后要全部再重置
        for(Node node:nodeList.values()){
            node.setIsTraverse(false);
        }
    }

    //遍历
    public void goThrough(HashMap<Integer,Node> nodeList,Node node,TreeItem treeItem){
        for(int i=0;i<node.getSonNodeList().size();i++){
            TreeItem item =new TreeItem<>(nodeList.get((int)node.getSonNodeList().get(i)).getContent());
            treeItem.getChildren().add(item);
            item.setExpanded(true);
            node.setIsTraverse(true);
            //打开时：这里需要通过得到nodeList直接画树
            goThrough(nodeList,nodeList.get((int)node.getSonNodeList().get(i)),item);
        }
    }
    public void drawSmallPane() {
        //如果根据每个结点来画，许多初始定义的大小长度都需要改变等，需要重新布局而且本来就是根据绘图区来画的
        /*
        1.直接把绘图区变成图片；2.按比例缩小；3.贴在缩略区的面板
         */
        //获得绘图区图片
        smallPane.getChildren().clear();
        double[] boarder = this.nodeArranger.getBoarder();
        double startX = boarder[0],startY = boarder[1];
        double width = boarder[2]-boarder[0]+100,height = boarder[3]-boarder[1]+100;
        WritableImage writableImage =new WritableImage((int)width,(int)height);
        SnapshotParameters parameters = new SnapshotParameters();
        Rectangle2D rectangle = new Rectangle2D(startX,startY,width,height);
        parameters.setViewport(rectangle);
        writableImage = anchorPane.snapshot(parameters,writableImage);
        //将图片按比例缩小
        BufferedImage smallMindBuffered = SwingFXUtils.fromFXImage((Image) writableImage,null);
        Image smallMind = SwingFXUtils.toFXImage(smallMindBuffered,(WritableImage) writableImage);
        ImageView imageView = new ImageView();
        imageView.setImage(writableImage);
        imageView.setPreserveRatio(true);
//        final double scaleX = this.smallPane.getWidth() / width;
//        final double scaleY = this.smallPane.getHeight() /height ;
//        imageView.getTransforms().add(new Scale(scaleX, scaleY));
        imageView.fitWidthProperty().bind(this.smallPane.widthProperty());
        imageView.fitHeightProperty().bind(this.smallPane.heightProperty());
//        imageView.setFitWidth(this.smallPane.getWidth());
//        imageView.setFitHeight(this.smallPane.getHeight());
//        anchorPane.getChildren().add(imageView);
        //将图片贴在缩略区面板上
        String backColor = this.nodeArranger.getBackColor();
        smallPane.setStyle("-fx-background-color:"+backColor);
        smallPane.getChildren().add(imageView);
    }

    public void changTitle(){
        this.title.setText(this.toolArranger.getFileName());
    }

    //清空绘图区
    public void cleanAnchorPane(){
        this.anchorPane.getChildren().clear();
    }

    //不会用迭代器,随便了反正也没人看。
//    public void goThrough(Node node){//节点和展示的第几个
//        this.sum++;
//        Button button=setButton(node);//第几个按钮
//        node.setIsTraverse(true);//表示该节点已经遍历了
//        this.pane.getChildren().add(button);
//        for(int i=0;i<node.getSonNodeList().size();i++){
//            goThrough(this.nodeArranger.getNodeList().get((int)node.getSonNodeList().get(i)));
//        }
//    }
//    //设置按钮样式
//    public Button setButton(Node node){
//        Button button=new Button();
//        button.setText(node.getContent());//设置文字
//        button.setFont(new Font("System",10));//设置文字字体大小
//        button.setMinHeight(20);//设置最低高度
//        button.setLayoutX(computePositionX(node));//设置坐标x,下同
//        button.setLayoutY(computePositionY(node));//设置第sum个按钮的y位置
//        return button;
//    }
//    public int computePositionX(Node node){
//        int depth=node.getDepth();//获取节点层次
//        // 使用节点层次计算左距离=基础距离+缩进基础距离*（层次-1）
//        return 14+10*depth;
//    }
//    public int computePositionY(Node node){
//        return 14+(this.sum-1)*30;//按钮高度30 基础位置+按钮高度*第几个按钮
//    }

}